/*
 * Created on Jan 21, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.vectrics.user.client;


import org.vectrics.common.dom.MarshalUtils;
import org.vectrics.client.webclient.*;
import org.vectrics.user.*;
import java.util.Vector;
import java.util.Collection;
import org.vectrics.user.PersonSynch;
import org.apache.log4j.Logger;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.vectrics.VectricsException;


public class FindUsersCommand extends ServerCommandImpl {
	private transient static Logger log = Logger.getLogger(FindUsersCommand.class);
	public static String COMMAND_NAME = "findUserName";
	private String firstName;
	private String lastName;
	private Vector userHolderList = new Vector();


	public FindUsersCommand() {
		super();
	}

	public String getCommandCode() {
		return(FindUsersCommand.COMMAND_NAME);
	}

	protected void prepareRequestData(Element requestElement) {
		log.debug("prepareRequestData()");
		Element userElement = this.createElement(requestElement, "user-criteria");
		if (lastName != null)
			userElement.setAttribute("lastName", lastName);
		if (firstName != null)
		userElement.setAttribute("firstName", firstName);
	}

	public Collection getUserHolderList() {
		return(userHolderList);
	}

	protected void processResponse(Element dataElement) {
		log.debug("processResponse() - translating XML document into user list");
		userHolderList.clear();
		//Iterator responseIter = dataElement.elementIterator();
		NodeList userListNodes = dataElement.getElementsByTagName("userList");
		for (int i = 0; i < userListNodes.getLength(); i++) {
			Element dataChildElement = (Element)userListNodes.item(i); 
			if (dataChildElement.getNodeName().equals("userList")) {
				NodeList userNodes = dataChildElement.getChildNodes();
				for (int j = 0; j < userNodes.getLength(); j++) {
					log.debug("processResponse() - found another user XML Element");
					Element userElement = (Element)userNodes.item(j);
					if (userElement.getNodeName().equals("userSummary")) {
						
						try {
							PersonSynch holder = new PersonSynch();
							MarshalUtils marshaller = MarshalUtils.getInstance();
							UserSummary userSummary = (UserSummary)marshaller.elementToObject(userElement, UserSummary.class);
							holder.setUserSummary(userSummary);
							userHolderList.add(holder);
						}
						catch (Exception de) {
							log.error("Exception - while translating DOM element to user object", de);
						}
						
					}
					else {
						log.error("Unexpected tag, expected 'user' got: " + userElement.getNodeName());
					}
				}
			}
			else {
				log.error("Error with data element child, expected 'userList', got: " + dataChildElement.getNodeName());
			}
		}
						
	}


	/**
	 * @return
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * @param firstName
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * @return
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * @param lastName
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

}
